package com.reliable.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionAlertsDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionHandlingDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionHandledDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionAlertsDetailDTO;
import com.reliable.api.dto.jurisdiction.ReliableJurisdictionSuccessDetailDTO;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionAlertsParam;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionHandlingParam;
import com.reliable.api.param.jurisdiction.ReliableJurisdictionHandledParam;
import com.reliable.service.domain.ReliableAddressCity;
import com.reliable.service.domain.ReliableJurisdictionAlerts;
import com.reliable.service.mapper.ReliableJurisdictionAlertsMapper;
import com.reliable.service.service.IReliableAddressCityService;
import com.reliable.service.service.IReliableJurisdictionAlertsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 属地研判主表 Service实现类
 *
 * @author ruoyi
 * * @date 2025-11-26
 */
@Service
public class ReliableJurisdictionAlertsServiceImpl extends ServiceImpl<ReliableJurisdictionAlertsMapper, ReliableJurisdictionAlerts> implements IReliableJurisdictionAlertsService {

    @Autowired
    private ReliableJurisdictionAlertsMapper reliableJurisdictionAlertsMapper;

    @Autowired
    private IReliableAddressCityService reliableAddressCityService;

    @Override
    public IPage<ReliableJurisdictionAlertsDTO> selectJurisdictionAlertsList(ReliableJurisdictionAlertsParam param) {
        Page<ReliableJurisdictionAlertsDTO> page = new Page<>(param.getPageNum(), param.getPageSize());
        return this.baseMapper.selectJurisdictionAlertsList(page, param);
    }

    @Override
    public IPage<ReliableJurisdictionHandlingDTO> selectJurisdictionHandlingList(ReliableJurisdictionHandlingParam param) {
        Page<ReliableJurisdictionHandlingDTO> page = new Page<>(param.getPageNum(), param.getPageSize());
        return this.baseMapper.selectJurisdictionHandlingList(page, param);
    }

    @Override
    public IPage<ReliableJurisdictionHandledDTO> selectJurisdictionHandledList(ReliableJurisdictionHandledParam param) {
        Page<ReliableJurisdictionHandledDTO> page = new Page<>(param.getPageNum(), param.getPageSize());
        return this.baseMapper.selectJurisdictionHandledList(page, param);
    }

    @Override
    public ReliableJurisdictionAlertsDetailDTO selectJurisdictionAlertsDetail(Long id) {
        ReliableJurisdictionAlertsDetailDTO detailDTO = reliableJurisdictionAlertsMapper.selectJurisdictionAlertsDetail(id);

// 假设 detailDTO 是一个 ReliableJurisdictionAlertsDetailDTO 实例
        if (detailDTO != null && detailDTO.getIpOccurrences() != null) {
            for (ReliableJurisdictionAlertsDetailDTO.IpOccurrence ipOccurrence : detailDTO.getIpOccurrences()) {
                if (ipOccurrence != null && ipOccurrence.getGeo() != null) {
                    String city = ipOccurrence.getGeo().getCity();
                    // 在这里对 city 进行判断操作
                    if (city != null && !city.isEmpty()) {
                        ReliableAddressCity cityLocationByName = reliableAddressCityService.getCityLocationByName(city);
                        if (cityLocationByName != null)
                        {
                            ipOccurrence.getGeo().setLng(cityLocationByName.getLng());
                            ipOccurrence.getGeo().setLat(cityLocationByName.getLat());
                        }
                    }
                }
            }
        }
        return  detailDTO;
    }

    @Override
    public ReliableJurisdictionSuccessDetailDTO selectJurisdictionSuccessDetail(Long id) {
        return reliableJurisdictionAlertsMapper.selectJurisdictionSuccessDetail(id);
    }
}